package com.guoguo.tangshisan.service;

import com.guoguo.tangshisan.util.GraphicsUtils;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.stereotype.Service;
import org.springframework.util.ClassUtils;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;

@Service
public class PosterService {

    /**
     * 创建海报图
     */
    public void createPoster() {
        String rootDir = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "static";
        try {
            BufferedImage bg1 = ImageIO.read(new File(rootDir + "/bg1-2.png"));
            BufferedImage bg2 = ImageIO.read(new File(rootDir + "/bg2.png"));
            BufferedImage head = ImageIO.read(new File(rootDir + "/head.png"));
            BufferedImage code = ImageIO.read(new File(rootDir + "/code.jpg"));

            // 绘制头像
            Graphics2D g1 = bg2.createGraphics();
            Ellipse2D.Double shape = new Ellipse2D.Double(60, 127, 167, 167);
            g1.setClip(shape);
            g1.drawImage(head, 60, 127, 167, 167, null);
            g1.setClip(null);

            // 绘制昵称
            Font font = new Font("微软雅黑", Font.BOLD, 40);
            g1.setFont(font);
            g1.setColor(new Color(66, 66, 66));
            // 设置昵称居中
            String text = "一天到晚游泳的鱼";
            g1.drawString(text, 282, 229);

            // 绘制邀请规则
            font = new Font("微软雅黑", Font.PLAIN, 35);
            g1.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g1.setFont(font);
            g1.setColor(new Color(88, 88, 88));
            text = "邀请规则";
            g1.drawString(text, 68, 421);

            font = new Font("微软雅黑", Font.PLAIN, 30);
            g1.setFont(font);
            text = "生活赋予我们一种巨大的和无限高贵的礼品，这就是青春：充满着力量，充满着期待志愿，充满着求知和斗争的志向，充满着希望信心和青春。";
            GraphicsUtils.drawStringWithFontStyleLineFeed(g1, text, 68, 491, font);

            // 绘制虚线
            float[] dash1 = {2f, 0f, 2f};
            BasicStroke bs1 = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1.0f, dash1, 2f);
            g1.setStroke(bs1);
            g1.setColor(new Color(229, 229, 229));
            g1.drawLine(50, 849, 850, 849);

            // 绘制长按识别二维码文字
            font = new Font("微软雅黑", Font.PLAIN, 35);
            g1.setFont(font);
            g1.setColor(new Color(88, 88, 88));
            text = "长按识别二维码";
            g1.drawString(text, 64, 1068);

            // 绘制二维码图片
            System.out.println(code.getWidth() / 2);
            System.out.println(code.getHeight() / 2);
            g1.drawImage(code, 610, 880, 235, 267, null);

            g1.dispose();


            // 绘制图片
            Graphics2D g2 = bg1.createGraphics();
            g2.drawImage(bg2, 112, 336, bg2.getWidth(), bg2.getHeight(), null);
            g2.dispose();

            // bufferedimage 转换成 inputstream
            ByteArrayOutputStream bs = new ByteArrayOutputStream();
            ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
            ImageIO.write(bg1, "jpg", imOut);
            InputStream inputStream = new ByteArrayInputStream(bs.toByteArray());

            Thumbnails.of(inputStream).scale(0.6f).outputQuality(1f).toFile("C:\\Users\\qwer\\Desktop\\a\\poster.png");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
